import json
import time
from urllib.parse import urlencode
from urllib.request import urlopen

import androidhelper

hello_msg = "Welcome to Coach Timing App"
list_title = "Here is your list of athletes:"
quit_msg = "Quitting Coach App"
web_server = "http://10.1.161.32:8080"
get_names_cgi = "/cgi-bin/generate_names.py"
get_data_cgi = "/cgi-bin/generate_data.py"


def send_to_server(url, post_data=None):
    if post_data:
        page = urlopen(url, urlencode(post_data).encode("utf-8"))
    else:
        page = urlopen(url)
    return page.read().decode("utf-8")


app = androidhelper.Android()


def status_update(msg, how_long=2):
    app.makeToast(msg)
    time.sleep(how_long)


status_update(hello_msg)

athlete_names = sorted(json.loads(send_to_server(web_server + get_names_cgi)))

app.dialogCreateAlert(list_title)
app.dialogSetSingleChoiceItems(athlete_names)
app.dialogSetPositiveButtonText("Select")
app.dialogSetNegativeButtonText("Quit")
app.dialogShow()

resp = app.dialogGetResponse().result
if resp["which"] in ("positive"):
    selected_athlete = app.dialogGetSelectedItems().result[0]
    which_athlete = athlete_names[selected_athlete]
    athlete = json.loads(send_to_server(web_server + get_data_cgi, {"which_athlete": which_athlete}))
    athlete_title = athlete["name"] + " " + athlete["DOB"] + " top 3 times:"
    app.dialogCreateAlert(athlete_title)
    app.dialogSetItems(athlete["Top3"])
    app.dialogSetPositiveButtonText("OK")
    app.dialogShow()
    resp = app.dialogGetResponse().result

status_update(quit_msg)
